三項演算子で throw したい
TL;DR
code:throw-in-expression.js
expression ? truthy : (() => { throw new Error() })()
ことの始まり
https://tweet-card.now.sh/1397838380445028353.png?lang=en&theme=dark&scale=1 https://twitter.com/dll7/status/1397838380445028353
https://tweet-card.now.sh/1397839508930842626.png?lang=en&theme=dark&scale=1 https://twitter.com/rokoucha/status/1397839508930842626
https://tweet-card.now.sh/1397840729246818310.png?lang=en&theme=dark&scale=1 https://twitter.com/dll7/status/1397840729246818310
https://tweet-card.now.sh/1397841138678059012.png?lang=en&theme=dark&scale=1 https://twitter.com/rokoucha/status/1397841138678059012
検証しました
code:throw-in-expression.ts
function hoge(piyo: string|number): void {
const puni: number = typeof piyo === 'string' ? piyo.length : (()=>{throw new Error('Invalid types')})();
const isPiyoString: string = piyo
}
普通に守ってくれた
https://tweet-card.now.sh/1397845962274394118.png?lang=en&theme=dark&scale=1 https://twitter.com/dll7/status/1397845962274394118
いいはなし。
ちなみに
https://tweet-card.now.sh/1397839234585677825.png?lang=en&theme=dark&scale=1 https://twitter.com/dll7/status/1397839234585677825
https://tweet-card.now.sh/1397839493026127874.png?lang=en&theme=dark&scale=1 https://twitter.com/dll7/status/1397839493026127874
https://tweet-card.now.sh/1397840469221011461.png?lang=en&theme=dark&scale=1 https://twitter.com/dll7/status/1397840469221011461
まあ確かにどこまで式にするねんという感じはする